package com.enjoyor.soa.traffic.server.efoms.dubbo.manage;

import com.alibaba.dubbo.config.annotation.Service;
import com.enjoyor.soa.traffic.api.dto.efoms.DeviceStatusInfoDto;
import com.enjoyor.soa.traffic.core.efoms.domain.DeviceStatusInfo;
import com.enjoyor.soa.traffic.core.efoms.service.IDevStatusInfoService;
import com.enjoyor.soa.traffic.frame.support.util.DomainUtil;
import com.enjoyor.soa.traffic.util.enums.EnumAppCode;
import com.enjoyor.soa.traffic.util.helper.LoggerHelper;
import com.enjoyor.soa.traffic.util.helper.ReflectHelper;
import com.enjoyor.soa.traffic.util.helper.ResultHelper;
import com.enjoyor.soa.traffic.util.pojo.ResultPojo;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

@Service(timeout = 6000)
public class ManageDevStatusInfoService{

    @Autowired
    private IDevStatusInfoService devStatusInfoService;
    /**
     * 新增设备状态信息表（关键字段：设备编号、设备名称、设备类别、设备类别名称、设备状态编号、设备装态）
     *
     * @param deviceStatusInfoDto
     * @param username
     * @return
     */
    public ResultPojo insertDevStatusInfo(DeviceStatusInfoDto deviceStatusInfoDto, String username) {
    	try {
	        if(deviceStatusInfoDto==null||deviceStatusInfoDto.dtoIsEmpty())
	            return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
	        DeviceStatusInfo deviceStatusInfo = ReflectHelper.beanToBean(deviceStatusInfoDto,DeviceStatusInfo.class);
	        devStatusInfoService.insertDevStatusInfo(deviceStatusInfo,username);
	        return ResultHelper.getResult(EnumAppCode.SUCCESS);
    	} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }

    /**
     * 报修、维修保存设备状态信息表
     * @param devId 设备编号
     * @param devName 设备名称
     * @param devTypeCode 设备类别
     * @param devTypeName 设备类别名称
     * @param repStatusCode 维修状态编号
     * @param repStatusName 维修状态名称
     * @param username 操作人
     * @return
     */
	public ResultPojo saveDevStatusInfo(String devCategoryCode, String devCategoryName, String devTypeCode, 
			String devTypeName, String devId, String devName, String repStatusCode, String repStatusName, 
			String devStatusCode, String devStatusName, String username) {
		try {
			DeviceStatusInfo deviceStatusInfo = new DeviceStatusInfo(devCategoryCode, devCategoryName, devTypeCode, 
					devTypeName,devId, devName, repStatusCode, repStatusName, devStatusCode, devStatusName);
			devStatusInfoService.insertDevStatusInfo(deviceStatusInfo, username);
	        return ResultHelper.getResult(EnumAppCode.SUCCESS);
		} catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
    
    /**
     * 修改设备状态信息表（关键字段：设备类别、设备Id、状态信息）
     *
     * @param deviceStatusInfoDto
     * @param username
     * @return
     */
    public ResultPojo updateDevStatusInfo(DeviceStatusInfoDto deviceStatusInfoDto, String username) {
        try {
	    	//if(deviceStatusInfoDto==null||deviceStatusInfoDto.idIsEmpty())
	          //  return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
	        DeviceStatusInfo deviceStatusInfo = ReflectHelper.beanToBean(deviceStatusInfoDto, DeviceStatusInfo.class);
	        devStatusInfoService.updateDevStatusInfo(deviceStatusInfo,username);
	        return ResultHelper.getResult(EnumAppCode.SUCCESS);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }

    /**
     * 获取设备状态信息
     * @param devCategoryCode 设备类别编号
     * @param devTypeCode 设备类型编号
     * @param devId     设备编号
     * @return
     */
    public ResultPojo getDevStatusInfo(String devCategoryCode, String devTypeCode, String devId ) {
        try {
	    	if(devTypeCode==null || devId ==null)
	            return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
	    	DeviceStatusInfo deviceStatusInfo = devStatusInfoService.getDevStatusInfo(devCategoryCode, devTypeCode, devId );
	    	DeviceStatusInfoDto deviceStatusInfoDto = ReflectHelper.beanToBean(deviceStatusInfo, DeviceStatusInfoDto.class);
	        return ResultHelper.getResult(deviceStatusInfoDto);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }

    /**
     * 获取设备状态信息
     * @param devStatusId
     * @return
     */
    public ResultPojo getDevStatusInfoById(String devStatusId) {
        try {
	    	if(devStatusId==null)
	            return ResultHelper.getResult(EnumAppCode.PARAM_ERR);
	    	
	    	DeviceStatusInfo deviceStatusInfo = devStatusInfoService.getDevStatusInfoById(devStatusId);
	    	DeviceStatusInfoDto deviceStatusInfoDto = ReflectHelper.beanToBean(deviceStatusInfo, DeviceStatusInfoDto.class);
	        return ResultHelper.getResult(deviceStatusInfoDto);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
    
    /**
     * 获取设备状态列表信息
     * @param devCategoryCode 设备类别编号
     * @param devTypeCode 设备类型编号
     * @param devId  设备编号
     * @param devName 设备名称
     * @param devStatusCode 设备状态编号
     * @param repStatusCode 报修状态编号
     * @return
     */
	public ResultPojo getDeviceStatusList(String devStatusId, String devCategoryCode, String devTypeCode, 
			String devId, String devName, String devStatusCode, String repStatusCode) {
        try {
	    	List<DeviceStatusInfo> deviceStatusInfoList = devStatusInfoService.selectDeviceStatusList(devStatusId, 
	    			devCategoryCode, devTypeCode, devId, devName, devStatusCode, repStatusCode);
	    	List<DeviceStatusInfoDto> deviceStatusInfoDtoList = ReflectHelper.listToList(deviceStatusInfoList, 
	    			DeviceStatusInfoDto.class);
	        return ResultHelper.getResult(deviceStatusInfoDtoList);
        } catch (Exception e) {
            LoggerHelper.LOG.error(e);
            return ResultHelper.getResult(e);
        }
    }
}
